package com.example.firstapp.service

import android.annotation.SuppressLint
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.Service
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Build
import android.os.IBinder
import androidx.core.app.NotificationCompat
import com.example.firstapp.MainActivity
import com.example.firstapp.R

class QiantaiService : Service() {

    override fun onBind(intent: Intent): IBinder {
        TODO("Return the communication channel to the service.")
    }

    override fun onCreate() {
        super.onCreate()
        println(2)
        // 创建通知渠道
        val manager = getSystemService(Context.NOTIFICATION_SERVICE) as
                NotificationManager
        println(3)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel("my_service", "前台Service通知",
                NotificationManager.IMPORTANCE_DEFAULT)
            manager.createNotificationChannel(channel)
        }
        println(4)
        val intent = Intent(this, MainActivity::class.java)
        println(4.1)
        val pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)
        println(4.5)
        val notification = NotificationCompat.Builder(this, "my_service")
            .setContentTitle("服务运行中")
            .setContentText("这是一则服务运行通知")
            .setSmallIcon(R.drawable.c)
            .setStyle(NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(resources, R.drawable.c)))
            .setContentIntent(pi)
            .build()
        println(5)
        startForeground(1, notification)
        println(5.1)
    }
}